# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import pymongo
from itemadapter import ItemAdapter
from pymongo.errors import DuplicateKeyError
from DangDangCrawl.items import  DangdangcrawlItem
from DangDangCrawl.settings import MONGO_URI, MONGO_PORT, MONGO_DATABASE


# class DangdangcrawlPipeline:
#     def process_item(self, item, spider):
#         return item

# class MongoDBPipeline(object):
#     def __init__(self):
#         client = pymongo.MongoClient(MONGO_URI, MONGO_PORT)
#         db = client[MONGO_DATABASE]
#         self.Tweets = db["dd_collect"]
#
#     def process_item(self, item, spider):
#         """ 判断item的类型，并作相应的处理，再入数据库 """
#
#         if isinstance(item, DangdangcrawlItem):
#              self.insert_item(self.Tweets, item)
#         return item
#
#     @staticmethod
#     def insert_item(collection, item):
#         try:
#             collection.insert(dict(item))
#         # 这个的前提是 dang_item 在for 循环外面
#         except DuplicateKeyError:
#             """
#             说明有重复数据
#             """
#             pass


# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
from twisted.enterprise import adbapi


class DgredisPipeline:
    def __init__(self, dbpool):
        self.dbpool = dbpool

    @classmethod
    def from_settings(cls, settings):
        adbparams = dict(
            host=settings['MYSQL_HOST'],
            db=settings['MYSQL_DB'],
            user=settings['MYSQL_USER'],
            password=settings['MYSQL_PASS'],
            charset='utf8mb4',
            cursorclass=pymysql.cursors.DictCursor
        )
        dbpool = adbapi.ConnectionPool('pymysql', **adbparams)
        return cls(dbpool)

    def process_item(self, item, spider):
        query = self.dbpool.runInteraction(self.do_insert, item)  # 指定操作方法和操作数据
        # 添加异常处理
        query.addCallback(self.handle_error)  # 处理异常

    def do_insert(self, cursor, item):
        # 对数据库进行插入操作，并不需要commit，twisted会自动commit
        insert_sql = """
        insert into dangdang(BookBigCategory,BookSmallCategory,BookTitle,BookAuthor,BookIntroduction,BookPrice,BookScore,BookComment,BookDate,BookPublish,BookPicture,BookUrl) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
                    """
        data=item['BookDate']
        if data==None:

            re_date=data
        else:
            re_date=data.replace('/','')
        cursor.execute(insert_sql, (
            item['BookBigCategory'], item['BookSmallCategory'], item['BookTitle'], item['BookAuthor'], item['BookIntroduction'],
            item['BookPrice'],
            item['BookScore'], item['BookComment'], re_date, item['BookPublish'], item['BookPicture'], item['BookUrl']))

    def handle_error(self, failure):
        if failure:
            print('插入数据失败')

